package com.lancoo.znbkxx.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.adapter.WrongItemAdapter;
import com.lancoo.znbkxx.beans.WrongFragmentBean;
import com.lancoo.znbkxx.beans.WrongItemListBean;
import com.lancoo.znbkxx.uis.PhoneMainActivity;

import java.util.ArrayList;
import java.util.List;

import znbkCNtxl.CNtxlActivity;
import znbkCNtxl.GetNtxlJson.PostCNtxlJsonBody;

/**
 * Create by TangJun on 2019/2/16
 */
public class WrongQuestionFragment  extends Fragment implements View.OnClickListener{
    private LinearLayout llToday;
    private LinearLayout llConsolidate;
    private LinearLayout llUnConsolidate;
    private TextView tvToday;
    private TextView tvTodayN;
    private TextView tvConsolidate;
    private TextView tvConsolidateN;
    private TextView tvUnConsolidate;
    private TextView tvUnConsolidateN;
    private WrongFragmentBean wrongFragmentBean;
    private TextView tvHead;
    private TextView tvHeadN;
    private RecyclerView recyclerView;
    private List<WrongItemListBean> tList;
    private List<WrongItemListBean> dList;
    private List<WrongItemListBean> yList;
    private ImageView ivNoDate;
    private WrongItemAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wrong, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findViews(view);
    }

  /*  @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        ((PhoneMainActivity)context).initData();
    }*/

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((PhoneMainActivity)getActivity()).getWrongFragment();
    }

    public void setText(WrongFragmentBean bean){
        wrongFragmentBean = bean;
        tvToday.setText(bean.getWrongItemTodayNum()+"");
        tvUnConsolidate.setText(bean.getWrongItemNum()+"");
        tvConsolidate.setText(bean.getWrongItemReviewedNum()+"");
        tvHeadN.setText(wrongFragmentBean.getWrongItemTodayNum()+"");
    }

    public static List<PostCNtxlJsonBody.QuesIDBean> QuesID;
    public static List<PostCNtxlJsonBody.QuesIDBean> getQuesID() {
        return QuesID;
    }
    public static void setQuesID(List<PostCNtxlJsonBody.QuesIDBean> quesID) {
        QuesID = quesID;
    }
    public void setList(WrongFragmentBean bean){
        tList = new ArrayList<>();
        dList = new ArrayList<>();
        yList = new ArrayList<>();
        for (int i = 0; i < bean.getModelWrongItemTypeListInfo().size(); i++ ){
            switch (bean.getModelWrongItemTypeListInfo().get(i).getWrongItemType()){
                case 1:
                    for (int j = 0;j < bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().size();j++){
                        WrongItemListBean listBean = new WrongItemListBean();
                        listBean.setItemNum(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getItemNum());
                        //listBean.setQuesIds(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesIds());
                        listBean.setQuesTypeName(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesTypeName());
                        listBean.setQuesTypeID(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesTypeID());
                        tList.add(listBean);
                    }
                    break;
                case 2:
                    for (int j = 0;j < bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().size();j++){
                        WrongItemListBean listBean = new WrongItemListBean();
                        listBean.setItemNum(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getItemNum());
                        //listBean.setQuesIds(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesIds());
                        listBean.setQuesTypeName(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesTypeName());
                        listBean.setQuesTypeID(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesTypeID());
                        dList.add(listBean);
                    }
                    break;
                case 3:
                    for (int j = 0;j < bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().size();j++){
                        WrongItemListBean listBean = new WrongItemListBean();
                        listBean.setItemNum(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getItemNum());
                        //listBean.setQuesIds(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesIds());
                        listBean.setQuesTypeName(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesTypeName());
                        listBean.setQuesTypeID(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesTypeID());
                        yList.add(listBean);
                    }
                    break;
            }
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new WrongItemAdapter(tList, new WrongItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                PhoneMainActivity.setCNtxlModel(1);
                Log.e("==adapter==","=============CNtxl_wrongIndex=============="+PhoneMainActivity.CNtxl_wrongIndex);
                Log.e("==adapter==","=============position=============="+position);
                QuesID = new ArrayList<>();
                int ItemNum = wrongFragmentBean.getModelWrongItemTypeListInfo().get(PhoneMainActivity.CNtxl_wrongIndex).getModelWrongItemListInfo().get(position).getItemNum();
                for(int k=0;k<ItemNum;k++){
                    PostCNtxlJsonBody.QuesIDBean QuesIDBean = new PostCNtxlJsonBody.QuesIDBean();
                    String QuesId = wrongFragmentBean.getModelWrongItemTypeListInfo().get(PhoneMainActivity.CNtxl_wrongIndex).
                            getModelWrongItemListInfo().get(position).getQuesIds().get(k).getQuesId();
                    String UpdateTime = wrongFragmentBean.getModelWrongItemTypeListInfo().get(PhoneMainActivity.CNtxl_wrongIndex).
                            getModelWrongItemListInfo().get(position).getQuesIds().get(k).getUpdateTime();
                    QuesIDBean.setQuesId(QuesId);
                    QuesIDBean.setUpdateTime(UpdateTime);
                    QuesID.add(k,QuesIDBean);
                    Log.e("==BODY=","========setQuesId======setQuesId======"+QuesId);
                }
                setQuesID(QuesID);

                Intent intent = new Intent();
                intent.setClass(getActivity(), CNtxlActivity.class);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        })
        /* {
            @Override
            protected void fillItemData(ViewHolder viewHolder, int position) {
                if (position % 5 == 0){
                    viewHolder.setImageForView(R.mipmap.icon_single);
                }else if (position % 5 == 1){
                    viewHolder.setImageForView(R.mipmap.icon_finish);
                }else if (position % 5 == 2){
                    viewHolder.setImageForView(R.mipmap.icon_translate);
                }else if (position % 5 == 3){
                    viewHolder.setImageForView(R.mipmap.icon_read);
                }else if (position % 5 == 4){
                    viewHolder.setImageForView(R.mipmap.icon_composition);
                }
            }
        }*/;
        recyclerView.setAdapter(adapter);
        if (tList.isEmpty()){
            ivNoDate.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }else {
            if(ivNoDate!=null) {
                ivNoDate.setVisibility(View.GONE);
            }
            recyclerView.setVisibility(View.VISIBLE);
            adapter.updateData(tList);
        }

    }
    private void findViews(View view) {
        llToday = view.findViewById(R.id.ll_today);
        llUnConsolidate = view.findViewById(R.id.ll_unconsolidate);
        llConsolidate = view.findViewById(R.id.ll_consolidate);
        tvToday = view.findViewById(R.id.tv_today);
        tvTodayN = view.findViewById(R.id.tv_today_n);
        tvConsolidate = view.findViewById(R.id.tv_consolidate);
        tvConsolidateN = view.findViewById(R.id.tv_consolidate_n);
        tvUnConsolidate = view.findViewById(R.id.tv_unconsolidate);
        tvUnConsolidateN = view.findViewById(R.id.tv_unconsolidate_n);
        recyclerView = view.findViewById(R.id.rv_list);
        ivNoDate = view.findViewById(R.id.iv_nodate);
        tvHead = view.findViewById(R.id.tv_head);
        tvHead.setText(tvTodayN.getText());
        tvHeadN = view.findViewById(R.id.tv_problemNum);
        llToday.setBackgroundResource(R.mipmap.today_select_bg);
        tvToday.setTextSize(32);
        tvTodayN.setTextSize(12);
        llToday.setOnClickListener(this);
        llUnConsolidate.setOnClickListener(this);
        llConsolidate.setOnClickListener(this);
    }

//    private int CNtxl_wrongIndex = 0;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_today:
                PhoneMainActivity.CNtxl_wrongIndex = 0;
                llToday.setBackgroundResource(R.mipmap.today_select_bg);
                tvToday.setTextSize(32);
                tvTodayN.setTextSize(12);
                llConsolidate.setBackgroundResource(R.mipmap.consolidate_bg);
                tvConsolidate.setTextSize(24);
                tvConsolidateN.setTextSize(10);
                llUnConsolidate.setBackgroundResource(R.mipmap.unconsolidate_bg);
                tvUnConsolidate.setTextSize(24);
                tvUnConsolidateN.setTextSize(10);
                tvHeadN.setText(wrongFragmentBean.getWrongItemTodayNum()+"");
                tvHead.setText("今日错题");
                if (tList.isEmpty()){
                    ivNoDate.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                }else {
                    recyclerView.setVisibility(View.VISIBLE);
                    ivNoDate.setVisibility(View.GONE);
                    adapter.updateData(tList);
                }
                break;
            case R.id.ll_unconsolidate:
                PhoneMainActivity.CNtxl_wrongIndex = 1;
                llUnConsolidate.setBackgroundResource(R.mipmap.unconsolidate_select_bg);
                tvUnConsolidate.setTextSize(32);
                tvUnConsolidateN.setTextSize(12);
                llToday.setBackgroundResource(R.mipmap.today_bg);
                tvToday.setTextSize(24);
                tvTodayN.setTextSize(10);
                llConsolidate.setBackgroundResource(R.mipmap.consolidate_bg);
                tvConsolidate.setTextSize(24);
                tvConsolidateN.setTextSize(10);
                tvHeadN.setText(wrongFragmentBean.getWrongItemNum()+"");
                tvHead.setText("未巩固错题");
                if (dList.isEmpty()){
                    ivNoDate.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                }else {
                    recyclerView.setVisibility(View.VISIBLE);
                    ivNoDate.setVisibility(View.GONE);
                    adapter.updateData(dList);
                }
                break;
            case R.id.ll_consolidate:
                PhoneMainActivity.CNtxl_wrongIndex = 2;
                llConsolidate.setBackgroundResource(R.mipmap.consolidate_select_bg);
                tvConsolidate.setTextSize(32);
                tvConsolidateN.setTextSize(12);
                llToday.setBackgroundResource(R.mipmap.today_bg);
                tvToday.setTextSize(24);
                tvTodayN.setTextSize(10);
                llUnConsolidate.setBackgroundResource(R.mipmap.unconsolidate_bg);
                tvUnConsolidate.setTextSize(24);
                tvUnConsolidateN.setTextSize(10);
                tvHeadN.setText(wrongFragmentBean.getWrongItemReviewedNum()+"");
                tvHead.setText("已巩固错题");
                if (yList.isEmpty()){
                    ivNoDate.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                }else {
                    recyclerView.setVisibility(View.VISIBLE);
                    ivNoDate.setVisibility(View.GONE);
                    adapter.updateData(yList);
                }
                break;
        }
    }

    public void setCurrentCNtxl(){
        if(PhoneMainActivity.CNtxl_wrongIndex==0){
            llToday.performClick();
        }else if(PhoneMainActivity.CNtxl_wrongIndex==1){
            llUnConsolidate.performClick();
        }else if(PhoneMainActivity.CNtxl_wrongIndex==2){
            llConsolidate.performClick();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("onResume测试","===========测试下错题的信息============"+PhoneMainActivity.CNtxl_wrongIndex);
    }
}
